/**
 * dutil: RandomUtil.java
 */

package net.acoder.util;

/**
 * @author Daniel
 * @date 2009-11-27 上午09:35:07
 * 
 */
public class RandomUtil {
    public static String getRandomAsciiString(int min, int max) {
        return getRandomString(Constants.ASCII_STRING, min, max);
    }

    public static String getRandomString(String stuff, int min, int max) {
        if (StringUtil.isEmpty(stuff)) {
            throw new IllegalArgumentException("stuff can't be null.");
        }
        return getRandomString(stuff.toCharArray(), min, max);
    }

    public static String getRandomString(char[] cs, int min, int max) {
        int len = getRandomInt(min, max);
        int csLen = cs.length;
        StringBuffer buf = new StringBuffer(len);
        int index = -1;
        for (int i = 0; i < len; i++) {
            index = getRandomInt(0, csLen);
            buf.append(cs[index]);
        }
        return buf.toString();
    }

    public static int getRandomInt(int min, int max) {
        return (int) (Math.random() * (max - min) + min);
    }

    public static void main(String[] args) {
        for (int i = 0; i < 10; i++)
            System.out.println(getRandomAsciiString(5, 20));
    }
}
